﻿#include "DialogFrame.h"
#include "ui_DialogFrame.h"
#pragma execution_character_set("utf-8")
class DialogFrame::Internal
{
public:
	void initControl()
	{
		setMaxBtnVisible(false);
	}
	void setMaxBtnVisible(bool isVisible)
	{
		ui.btnMenu_Max->setVisible(isVisible);
		ui.btnMenuRestore->setVisible(!isVisible);
	}
public:
	Ui::DialogFrame ui;
};

DialogFrame::DialogFrame(QWidget* parent) :
	FramelessDialog(parent),
	p(new Internal())
{
	p->ui.setupUi(this);
	p->initControl();

	//设置标题栏控件
	p->ui.labTitle->setText("对话框");
	this->setWindowTitle(p->ui.labTitle->text());
	this->setTitleBar(p->ui.labTitle);
	//关联信号
	connect(this, SIGNAL(titleDblClick()), this, SLOT(titleDblClick()));
	connect(this, SIGNAL(windowStateChange(bool)), this, SLOT(windowStateChange(bool)));
}

DialogFrame::~DialogFrame()
{
	delete p;
	p = nullptr;
}

void DialogFrame::setTitle(QString title)
{
	p->ui.labTitle->setText(title);
}

bool DialogFrame::event(QEvent* event)
{
	if (event->type() == QEvent::Show)
	{
		if (this->isMaximized()) {
			p->setMaxBtnVisible(false);
		}
		else {
			p->setMaxBtnVisible(true);
		}
	}
	return QWidget::event(event);
}

void DialogFrame::on_btnMenu_Min_clicked()
{
#ifdef Q_OS_MACOS
	this->setWindowFlags(this->windowFlags() & ~Qt::FramelessWindowHint);
#endif
	this->showMinimized();
}

void DialogFrame::on_btnMenu_Max_clicked()
{

	this->showMaximized();
}

void DialogFrame::on_btnMenuRestore_clicked()
{
	this->showNormal();
}

void DialogFrame::on_btnMenu_Close_clicked()
{
	this->close();
}

void DialogFrame::titleDblClick()
{
	on_btnMenu_Max_clicked();
}

void DialogFrame::windowStateChange(bool max)
{
	if (max)
	{
		p->setMaxBtnVisible(false);
		//<! 最大化的时候，会把标题栏上方的部分位置挡住，所以做margin
		p->ui.horizontalLayout_2->setContentsMargins(9, 9, 6, 0); 
	}
	else
	{
		p->setMaxBtnVisible(true);
		p->ui.horizontalLayout_2->setContentsMargins(0, 0, 0, 0);
	}
}
